package one.chchy.demo.sprintsecurity;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;

/**
 * Created by 程辉 on 2017/6/19.
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ServerResponse<T> implements Serializable {
    private int code;
    private String msg;
    private T content;

    private ServerResponse(int code, T content){
        this.code = code;
        this.content = content;
    }

    private ServerResponse(int code, String msg, T content){
        this.code = code;
        this.msg = msg;
        this.content = content;
    }

    private ServerResponse(int code, String msg){
        this.code = code;
        this.msg = msg;
    }

    //使之不在json序列化结果当中
//    @JsonIgnore
//    public boolean isSuccess(){
//        return this.code == ResponseCode.SUCCESS.getCode();
//    }

    public int getCode(){
        return code;
    }
    public T getContent(){
        return content;
    }
    public String getMsg(){
        return msg;
    }


    public static <T> ServerResponse<T> createBySuccess(){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getDesc());
    }

    public static <T> ServerResponse<T> createBySuccessMessage(String msg){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg);
    }

    public static <T> ServerResponse<T> createBySuccess(T content){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),content);
    }

    public static <T> ServerResponse<T> createBySuccess(String msg,T content){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),msg,content);
    }

    public static <T> ServerResponse<T> createByError(String msg){
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(), msg);
    }

    public static <T> ServerResponse<T> createByError(String msg, T content){
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(), msg, content);
    }

    public static <T> ServerResponse<T> createByError(ResponseCode responseCode, String msg, T content){
        return new ServerResponse<T>(responseCode.getCode(), msg, content);
    }

    public static <T> ServerResponse<T> createByError(ResponseCode responseCode){
        return new ServerResponse<T>(responseCode.getCode(),responseCode.getDesc());
    }

    public static <T> ServerResponse<T> createByError(ResponseCode responseCode, String msg){
        return new ServerResponse<T>(responseCode.getCode(), msg);
    }
}
